﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace ImaginationFactory.Dialogs
{
    /// <summary>
    /// BatchProcessDialog.xaml 的交互逻辑
    /// </summary>
    /// 

    public partial class BatchProcessDialog : Window
    {
        private BatchProcessor batchProcessor;

        MainWindow mWindowTemp;
        public string pathTemp;
        private string targetPath;
        private List<string> selectedFileList = new List<string>();
        private List<Parameter> selectedEffectList = new List<Parameter>();
        string[] pictureFile = { ".jpg", ".bmp", ".png", ".jpeg" }; 

        public BatchProcessDialog( MainWindow mw)
        {
            InitializeComponent();
            mWindowTemp = mw;

            batchProcessor = new BatchProcessor( mWindowTemp.ImageLibrary );
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //选取文件
        private void openFileButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbDlg = new FolderBrowserDialog();
            fbDlg.ShowDialog();
            pathTemp = fbDlg.SelectedPath.ToString();

            showTheFile(pathTemp);
        }

        //设置路径
        private void setButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbDlg = new FolderBrowserDialog();
            fbDlg.ShowDialog();
            targetPath  = fbDlg.SelectedPath.ToString();
            targetPathTextBox.Text = targetPath;

            batchProcessor.SetSavePath( targetPath );
        }

        //在listbox中显示该文件夹中的图片文件
        private void showTheFile(string selectedPath)
        {
            fileListBox.Items.Clear();

            try
            {
                foreach (string fileNames in System.IO.Directory.GetFiles(selectedPath))
                {
                    foreach (string type in pictureFile)
                    {
                        if (fileNames.LastIndexOf(type) == fileNames.Length - type.Length)
                        {
                            //对listbox的item进行布局

                            //对itemGrid进行布局
                            Grid itemGrid = new Grid();
                            RowDefinition row = new RowDefinition();
                            itemGrid.RowDefinitions.Add(row);
                            row = new RowDefinition();
                            itemGrid.RowDefinitions.Add(row);

                            
                            //属性设置
                            ListBoxItem newItem = new ListBoxItem();
                            newItem.Content = itemGrid;
                            newItem.Height = 150;
                            newItem.Width = 150;
                            
                            //面板上的控件初始化
                            System.Windows.Controls.Label fileNameLabel = new System.Windows.Controls.Label();
                            fileNameLabel.Content = fileNames.ToString();
                            itemGrid.Children.Add(fileNameLabel);
                            Grid.SetRow(fileNameLabel, 1);

                            Image fileImage = new Image();
                            BitmapImage bp = new BitmapImage();
                            bp.BeginInit();
                            bp.UriSource = new Uri(fileNames.ToString(), UriKind.Absolute);
                            bp.EndInit();
                            fileImage.Stretch = Stretch.Uniform;
                            fileImage.Source = bp;
                            itemGrid.Children.Add(fileImage);
                            Grid.SetRow(fileImage, 0);

                             //fileNames.ToString();
                            fileListBox.Items.Add(newItem);
                        }
                    }
                }             
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "in loading in showTheFile");
            }
        }


        #region 特效面板上的消息处理（check Button）
        Parameter gaussPara = new Parameter(); 
        Parameter lightPara = new Parameter();
        Parameter singcleColorPara = new Parameter();
        Parameter GTPara = new Parameter();
        Parameter colorPassagePara = new Parameter();
        Parameter contrastPara = new Parameter();
        Parameter sizePara = new Parameter();

        private void gaussCB_Checked(object sender, RoutedEventArgs e)
        {
            gaussPara.m_Kind = KindOfEffect.GussianBlur_image;
            effectInfo( ref gaussPara);

        }

        private void lightCB_Checked(object sender, RoutedEventArgs e)
        {
            lightPara.m_Kind = KindOfEffect.Light_Shade_image;
            effectInfo( ref lightPara);
        }

        private void singleColorCB_Checked(object sender, RoutedEventArgs e)
        {
            singcleColorPara.m_Kind = KindOfEffect.BlackAndWhite_image;
            effectInfo( ref singcleColorPara);
        }

        private void GTCB_Checked(object sender, RoutedEventArgs e)
        {
            GTPara.m_Kind = KindOfEffect.GT_Equal_image;
            effectInfo( ref GTPara);
        }

        private void colorPassageCB_Checked(object sender, RoutedEventArgs e)
        {
            colorPassagePara.m_Kind = KindOfEffect.Color_Passage_image;
            effectInfo( ref colorPassagePara);
        }

        private void contrastCB_Checked(object sender, RoutedEventArgs e)
        {
            contrastPara.m_Kind = KindOfEffect.ColorContrats_image;
            effectInfo( ref contrastPara);
        }

        private void sizeCB_Checked(object sender, RoutedEventArgs e)
        {
            sizePara.m_Kind = KindOfEffect.Scale_image;
            effectInfo( ref sizePara);
        }
        #endregion

        #region 特效面板上的消息处理：将用户设置的参数指定为处理函数的参数

        private string regularTest = "\\d";
        bool onGauss = false;
        bool onContr = false;
        bool onLight = false;
        bool onR = false;
        bool onB = false;
        bool onG = false;

        private void GaussTxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (GaussTxtBox.Text != "power")
            {
                if (GaussTxtBox.Text != "")
                {
                     onGauss = true;

                     if (Regex.IsMatch(GaussTxtBox.Text, regularTest) == true)
                     {
                         try
                         {
                             gaussPara.m_GuassianPower = (int)double.Parse(GaussTxtBox.Text);
                         }
                         catch (Exception ex)
                         {
                             System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                         }
                     }
                     else
                         System.Windows.Forms.MessageBox.Show("请输入数字");
              }
            }
        }

        private void ContrTxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (ContrTxtBox.Text != "contrast" )
            {

                if(ContrTxtBox.Text !="")
                    onContr = true;
                if ( Regex.IsMatch( ContrTxtBox.Text, regularTest ) == true || ContrTxtBox.Text == "" )
                {
                    try
                    {
                        contrastPara.m_ConstrastValue = double.Parse( ContrTxtBox.Text );
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                    }
                }
                else
                    System.Windows.Forms.MessageBox.Show("请输入数字");

            }
        }

        private void LightTxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (LightTxtBox.Text != "light")
            {
                if(LightTxtBox.Text != "")
                    onLight = true;
                if (Regex.IsMatch(LightTxtBox.Text, regularTest))
                {
                    try
                    {
                        lightPara.Light_Shade = (int)double.Parse( LightTxtBox.Text );
                    }

                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                    }
                }
                else
                    System.Windows.Forms.MessageBox.Show("请输入数字");

            }
        }

        private void GtxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ( GtxtBox.Text != "G")
            {
                if(GtxtBox.Text != "")
                    onG = true;
                if ( Regex.IsMatch(GtxtBox.Text, regularTest))
                {
                    try
                    {

                        colorPassagePara.ColorPassage_green = double.Parse( GtxtBox.Text );
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                    }
                }
                else
                    System.Windows.Forms.MessageBox.Show("请输入数字");
            }
        }

        private void RtxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (RtxtBox.Text != "R")
            {
                if(RtxtBox.Text != "")
                   onR = true;
                if (Regex.IsMatch(RtxtBox.Text, regularTest) == true)
                {
                    try
                    {

                        colorPassagePara.ColorPassage_red = double.Parse( RtxtBox.Text );
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                    }
                }
                else
                    System.Windows.Forms.MessageBox.Show("请输入数字");

            }
        }

        private void BtxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (BtxtBox.Text != "B")
            {
                if(BtxtBox.Text != "")
                   onB = true;

                if ( Regex.IsMatch(BtxtBox.Text, regularTest))
                {
                    try
                    {                        
                        colorPassagePara.ColorPassage_blue = double.Parse(BtxtBox.Text);
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                    }
                }
                else
                    System.Windows.Forms.MessageBox.Show("请输入数字");

            }
        }

        private void SizeHightTxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (Regex.IsMatch(SizeHightTxtBox.Text, regularTest))
            {
                try
                {
                    sizePara.Scale_y = double.Parse(SizeHightTxtBox.Text);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                }
            }
            else
                System.Windows.Forms.MessageBox.Show("请输入数字");


        }

        private void SizeWidthTxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Regex.IsMatch(SizeHightTxtBox.Text, regularTest))
            {
                try
                {
                    sizePara.Scale_x = double.Parse( SizeWidthTxtBox.Text );
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "请输入正确的格式");
                }
            }
            else
                System.Windows.Forms.MessageBox.Show("请输入数字");

        }
        #endregion


        //对选中文件名的添加
        private void fileListBox_Unselected(object sender, RoutedEventArgs e)
        {
            selectedFileList.Remove(fileListBox.SelectedItem.ToString());
        }

        //对选中文件名的删除
        private void fileListBox_Selected(object sender, RoutedEventArgs e)
        {
            selectedFileList.Add(fileListBox.SelectedItem.ToString());
        }

        private void effectInfo( ref Parameter paraTemp)
        {
            ListBoxItem newItem = new ListBoxItem();
            newItem.Content = paraTemp.m_Kind.ToString();
            effectInfoListBox.Items.Add(newItem);

            selectedEffectList.Add(paraTemp);
        }

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            int i;
            for( i=0; i<effectInfoListBox.Items.Count; i++)
            {
                if (effectInfoListBox.SelectedItem == effectInfoListBox.Items[i])
                    effectInfoListBox.Items.Remove(effectInfoListBox.Items[i]);
                    selectedEffectList.Remove(selectedEffectList[i]);
            }

        }

        private void GaussTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            GaussTxtBox.Text = "";
        }

        private void ContrTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            ContrTxtBox.Text = "";
        }

        private void LightTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            LightTxtBox.Text = "";
        }

        private void GtxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            GtxtBox.Text = "";
        }

        private void RtxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            RtxtBox.Text = "";
        }

        private void BtxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            BtxtBox.Text = "";
        }


        private void GaussTxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if( onGauss == false)
            GaussTxtBox.Text = "power";
        }

        private void ContrTxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if( onContr == false) 
            ContrTxtBox.Text = "contrast";
        }

        private void LightTxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if( onLight == false)
            LightTxtBox.Text = "light";
        }

        private void GtxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if( onG == false)
            GtxtBox.Text = "G";
        }

        private void RtxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if( onB == false)
            
            RtxtBox.Text = "R";
        }

        private void BtxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if ( onB == false)
            BtxtBox.Text = "B";
        }

        // 处理 
        private void buttonBatch_Click( object sender, RoutedEventArgs e )
        {
            batchProcessor.LoadFiles( pathTemp );    

            foreach ( Parameter param in selectedEffectList )
            {
                batchProcessor.AddEffects( param );
            }

            batchProcessor.StartProcess( 10 );
        }

    }
}
